﻿@using QLNS
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Home.cshtml";
    //List<PhongBan> listPhongBan = ViewBag.danhsachPB;
}
@{
   
    string tree = "<div id='jqxTreeListDept' style='float: left; margin-left: 0px;'>";
    
    List<PhongBan> root = QLNS.Models.PhongBanModels.GetDSPhongBan(0);
    if(root.Count>0)
    {
        tree += "<ul>"; 
        //Cap 1 la Don vi       
        foreach(var r in root)
        {

            tree += "<li item-expanded='true'  id='" + r.ID + "'>" + r.TenPhongBan;
            List<PhongBan> root1 = QLNS.Models.PhongBanModels.GetDSPhongBan(r.ID);
            
            if(root1.Count>0)
            {
                tree += "<ul>";                              
                foreach(var r1 in root1)
                {

                    tree += "<li id='" + r1.ID + "'>" + r1.TenPhongBan;
                    List<PhongBan> root2 = QLNS.Models.PhongBanModels.GetDSPhongBan(r1.ID);
                    if(root2.Count>0 )
                    {
                        tree += "<ul>";                                         
                        foreach(var r2 in root2)
                        {
                            tree += "<li  id='" + r2.ID + "'>" + r2.TenPhongBan;
                            List<PhongBan> root3 = QLNS.Models.PhongBanModels.GetDSPhongBan(r2.ID);                           
                            if(root3.Count>0 )
                            {
                                tree += "<ul>";                                               
                                foreach(var r3 in root3)
                                {
                                    tree += "<li id='" + r3.ID + "'>" + r3.TenPhongBan;
                                    List<PhongBan> root4 = QLNS.Models.PhongBanModels.GetDSPhongBan(r3.ID);
                                   
                                    if (root4.Count > 0 )
                                    {
                                        tree += "<ul>";                                       
                                        foreach (var r4 in root4)
                                        {
                                            tree += "<li id='"+r4.ID+"'>" + r4.TenPhongBan;                                           
                                            tree += "</li>";
                                        }
                                        tree += "</ul>";
                                    }
                                    tree += "</li>";
                                   
                                }
                                tree += "</ul>";
                            }
                            
                            tree += "</li>";
                        }
                        tree += "</ul>";
                    }
                    else
                    {
                        tree += "</li>";
                    } 
                }
                tree += "</ul>";           
            }
            else
            {
                tree += "</li>";
            }
        }
        tree += "</ul>";
    }
    tree += "</div>";
}
<script type="text/javascript">
    $(document).ready(function () {
        var theme = getDemoTheme();
        // Create jqxTree
        $('#jqxTreeListDept').jqxTree({ height: '600px', width: '95%', theme: theme });
        var contextMenu = $("#jqxMenuListDept").jqxMenu({ width: '180px', theme: theme, height: '100px', autoOpenPopup: false, mode: 'popup' });
        var clickedItem = null;

        var attachContextMenu = function () {
            // open the context menu when the user presses the mouse right button.
            $("#jqxTreeListDept li").on('mousedown', function (event) {
                var target = $(event.target).parents('li:first')[0];
                var rightClick = isRightClick(event);
                if (rightClick && target != null) {
                    $("#jqxTreeListDept").jqxTree('selectItem', target);
                    var scrollTop = $(window).scrollTop();
                    var scrollLeft = $(window).scrollLeft();
                    contextMenu.jqxMenu('open', parseInt(event.clientX) + 5 + scrollLeft, parseInt(event.clientY) + 5 + scrollTop);
                    return false;
                }
            });
        }
        attachContextMenu();
        $("#jqxMenuListDept").on('itemclick', function (event) {
            var item = $.trim($(event.args).text());
            switch (item) {
                case "Thêm mới":
                    var selectedItem = $('#jqxTreeListDept').jqxTree('selectedItem');
                    if (selectedItem != null) {
                        if (selectedItem.id > 0) {
                            document.location.href = '/Dept/Add/';
                            //$('#jqxTree1').jqxTree('addTo', { label: selectedItem.id }, selectedItem.element);
                            //attachContextMenu();
                        }
                    }
                    break;
                case "Cập nhật":
                    var selectedItem = $('#jqxTreeListDept').jqxTree('selectedItem');
                    if (selectedItem != null) {
                        if (selectedItem.id > 0) {
                            document.location.href = '/Dept/Update/' + selectedItem.id + '/';
                            //$('#jqxTree1').jqxTree('addTo', { label: selectedItem.id }, selectedItem.element);
                            //attachContextMenu();
                        }
                    }
                    break;
                case "Xóa":
                    var selectedItem = $('#jqxTreeListDept').jqxTree('selectedItem');
                    if (selectedItem != null) {
                        if (selectedItem.id > 0) {
                            $.get('/Dept/GetConfirmDelete/' + selectedItem.id, null,
                               function (data) {
                                   if (data > 0) {
                                       jqxAlert.alert('Tồn tại cán bộ, viên chức trực thuộc phòng ban này. Hãy xóa cán bộ trước!');
                                       //  
                                   }
                                   else {
                                       jqxConfirm.alert('Bạn có chắc muốn xóa phòng ban này khỏi danh sách?');
                                       $("#alert_button").click(function () {
                                           document.location.href = '/Dept/Delete/' + selectedItem.id + '/';
                                           jqxAlert._hide();
                                       });
                                   }
                               });
                            //$('#jqxTree1').jqxTree('addTo', { label: selectedItem.id }, selectedItem.element);
                            //attachContextMenu();
                        }
                    }
                    break;
            }
        });
        // disable the default browser's context menu.
        $(document).on('contextmenu', function (e) {
            if ($(e.target).parents('.jqx-tree').length > 0) {
                return false;
            }
            return true;
        });
        function isRightClick(event) {
            var rightclick;
            if (!event) var event = window.event;
            if (event.which) rightclick = (event.which == 3);
            else if (event.button) rightclick = (event.button == 2);
            return rightclick;
        }
    });
    </script>

<div class="toolbar">
<ul>
    <li><input class="tbtnBack" type="button" value="Quay về" onclick="javascript:history.back()"/>
    </li>
     @{ if (Session["Quyen"].ToString() == "1")
        {
    <li><input class="tbtnAdd" type="button" value="Thêm mới" onclick="javascript:window.location='/Dept/Add/'"/>
    </li>}}
</ul>
<div class="ttitle">Danh sách Phòng ban</div>
</div>
 @{ if (Session["Quyen"].ToString() == "1")
    {
<div id='jqxWidgetListCp'>@Html.Raw(tree)
        
        <div id='jqxMenuListDept'>
            <ul>
                <li>Thêm mới</li>
                <li>Cập nhật</li>
                <li>Xóa</li>                
            </ul>
        </div>
     </div>
    }}
